1445D - Divide and Sum - CodeForces Solution


combinatorics math sortings *1900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M=998244353;
const int N=1e6;
ll fact[N+2];
ll fast_pow(ll a,ll b){
    if(b==0)
        return 1;
    ll p=fast_pow(a,b/2);
    p=p*p%M;
    if(b&1)
        p=p*a%M;
    return p;
}
void init(){
    fact[0]=1;
    for(int i=1;i<=N;i++){
        fact[i]=fact[i-1]*i%M;
    }
}
ll nCr(ll n,ll r){
    return fact[n]*fast_pow(fact[r],M-2)%M*fast_pow(fact[n-r],M-2)%M;
}
int main(){
    init();
    int n;
    cin>>n;
    ll a[2*n+2];
    for(int i=0;i<2*n;i++){
        cin>>a[i];
    }
    sort(a,a+2*n);
    ll cost=0;
    for(int i=0;i<n;i++)
        cost-=a[i];
    for(int i=n;i<2*n;i++)
        cost+=a[i];
    cost=cost%M;
    cout<<cost*nCr(2*n,n)%M<<endl;
}


Comments

Submit
0 Comments
More Questions

834A - The Useless Toy
1407D - Discrete Centrifugal Jumps
1095B - Array Stabilization
291B - Command Line Arguments
1174B - Ehab Is an Odd Person
624B - Making a String
1064C - Oh Those Palindromes
1471A - Strange Partition
1746A - Maxmina
1746B - Rebellion
66C - Petya and File System
1746C - Permutation Operations
1199B - Water Lily
570B - Simple Game
599C - Day at the Beach
862A - Mahmoud and Ehab and the MEX
1525A - Potion-making
1744D - Divisibility by 2n
1744A - Number Replacement
1744C - Traffic Light
1744B - Even-Odd Increments
637B - Chat Order
546C - Soldier and Cards
18D - Seller Bob
842B - Gleb And Pizza
1746D - Paths on the Tree
1651E - Sum of Matchings
19A - World Football Cup
630P - Area of a Star
1030C - Vasya and Golden Ticket